package javaDemo12.网络编程;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerThread implements Runnable {
    private Socket s;
    public ServerThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        try {
            BufferedReader bf = new BufferedReader(new InputStreamReader(s.getInputStream()));
//            BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\copy.txt"));
            int count = 0;
            File file = new File("D:\\copy[" + count + "].txt");
            while(file.exists()){
                count++;
                file = new File("D:\\copy[" + count + "].txt");
            }
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));

            String line;
            while((line = bf.readLine()) != null && line.isEmpty()){
                bw.write(line);
                bw.newLine();
                bw.flush();
            }

            BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bwServer.write("数据已收到");
            bwServer.newLine();
            bwServer.flush();

            s.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
